"""
快速启动脚本
一键式启动系统
"""
import sys
import subprocess
from pathlib import Path

def check_dependencies():
    """检查依赖是否安装"""
    try:
        import fastapi
        import sentence_transformers
        import faiss
        import streamlit
        print("✅ 所有依赖已安装")
        return True
    except ImportError as e:
        print(f"❌ 缺少依赖: {e}")
        print("请运行: pip install -r requirements.txt")
        return False

def setup_directories():
    """创建必要的目录"""
    directories = [
        "data/raw",
        "data/processed",
        "models/faiss_indices",
        "logs"
    ]
    
    for dir_path in directories:
        Path(dir_path).mkdir(parents=True, exist_ok=True)
        print(f"✓ 创建目录: {dir_path}")

def main():
    print("=" * 60)
    print("🏥 医学文献智能检索与摘要平台 - 快速启动")
    print("=" * 60)
    print()
    
    # 检查依赖
    if not check_dependencies():
        sys.exit(1)
    
    # 设置目录
    print("\n设置项目目录...")
    setup_directories()
    
    # 显示菜单
    print("\n" + "=" * 60)
    print("请选择启动模式:")
    print("=" * 60)
    print("1. FastAPI服务器 (适合API调用和Vue.js前端)")
    print("2. Streamlit界面 (适合快速使用)")
    print("3. 桌面客户端 (Tkinter GUI)")
    print("4. 爬取数据")
    print("5. 构建向量索引")
    print("0. 退出")
    print("=" * 60)
    
    choice = input("\n请输入选项 (0-5): ").strip()
    
    if choice == "1":
        print("\n启动FastAPI服务器...")
        print("API文档地址: http://localhost:8000/docs")
        subprocess.run([sys.executable, "main.py", "server"])
    
    elif choice == "2":
        print("\n启动Streamlit界面...")
        print("访问地址: http://localhost:8501")
        subprocess.run([sys.executable, "main.py", "streamlit"])
    
    elif choice == "3":
        print("\n启动桌面客户端...")
        try:
            from client.app import main
            main()
        except ImportError:
            print("❌ 无法导入客户端模块")
            print("请确保所有依赖已正确安装")
    
    elif choice == "4":
        query = input("请输入搜索查询 (留空使用默认): ").strip()
        max_results = input("请输入最大结果数 (默认100): ").strip() or "100"
        
        try:
            max_results = int(max_results)
        except ValueError:
            max_results = 100
        
        subprocess.run([
            sys.executable, 
            "main.py", 
            "crawl", 
            "--query", query,
            "--max-results", str(max_results)
        ])
    
    elif choice == "5":
        print("\n构建向量索引...")
        subprocess.run([sys.executable, "main.py", "build-index"])
    
    elif choice == "0":
        print("\n再见！")
        sys.exit(0)
    
    else:
        print("\n❌ 无效选项")
        sys.exit(1)

if __name__ == "__main__":
    main()
